<template>
  <div>
    <el-dialog :title="info.isAdd ? '秒杀添加' : '秒杀修改'" :visible.sync="info.isshow" @close="close">
      <el-form :model="user">
        <el-form-item label="活动名称" label-width="120px">
          <el-input v-model="user.title"></el-input>
        </el-form-item>
        user:{{user}}
        <!-- 活动期限 -->
        <el-form-item label="活动期限" label-width="120px">
          <div class="block">
            <!-- 需要的是时间戳-->
            <el-date-picker
              v-model="value1"
              unlink-panels
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              @change="onClick"
            ></el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="一级分类" label-width="120px"  >
          <el-select v-model="user.first_cateid" @change="change_first_cate">
            <el-option
              v-for="item in cateList"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类" label-width="120px">
          <el-select v-model="user.second_cateid">
            <el-option
              v-for="item in second_cate_list"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品" label-width="120px">
          <el-input v-model="user.goodsid"></el-input>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="user.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add">添 加</el-button>
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { formClose } from "../../../mixins";
import {
  reqcatelist,
  reqseckinfo,
  reqseckadd,
  reqseckedit
} from "../../../http/api";
import { successAlert } from "../../../utils/alert";
export default {
  props: ["info"],
  mixins: [formClose],
  data() {
    return {
      user: {
        id: "",
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1
      },
      second_cate_list: [],
      value1:'',
      // thrid_cate_list:[],

      
    };
  },
  computed: {
    ...mapGetters({
      cateList: "cate/arr"

    })
  },
  mounted() {
    this.reqCateArr();
    // this.reqArr();
  },
  methods: {
    ...mapActions({
      reqCateArr: "cate/reqArr",
      reqArr: "seck/reqArr"


    }),
   

    change_first_cate() {
      //清空二级分类选中的值
      console.log(1);
      
      this.user.second_cateid = "";
      this.get_second_cate_list();
      
    },
    //  change_second_cate() {
    //   //清空三级分类选中的值
    //   this.user.third_cateid = "";
    //   this.get_third_cate_list();
    // },
    
    get_second_cate_list() {
      console.log(111);
      
      reqcatelist({ pid: this.user.first_cateid }).then(res => {
        if (res.data.code == 200) {
          this.second_cate_list = res.data.list;
          // console.log(res.data.list);
          
        }
      });
    },
      onClick(e) {
      this.user.begintime = +new Date(e[0]);
      this.user.endtime = +new Date(e[1]);
    },
    //  get_thrid_cate_list() {
    //   reqcatelist({ pid: this.user.second_cateid }).then(res => {
    //     if (res.data.code == 200) {
    //       this.third_cate_list = res.data.list;
    //     }
    //   });
    // },
    empty() {
      this.user = {
        id: "",
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1
      };
      this.second_cate_list = [];
      // this.thrid_cate_list = [];
    },
    getOne(id) {
      //发请求，并且赋值
      reqseckinfo({ id: id }).then(res => {
        if (res.data.code === 200) {
          this.user = res.data.list;
           this.user.id = id;
          this.value1 = [
            new Date(Number(this.user.begintime)),
            new Date(Number(this.user.endtime)),]
        }
      });
    },
    // 6点击修改按钮
    update() {
      // 6.2发送请求reqmemberedit
      reqseckedit(this.user).then(res => {
        if (res.data.code == 200) {
          //弹窗成功
          successAlert(res.data.msg);
          //弹窗内容清空
          this.empty();
          //弹窗消失
          this.info.isshow = false;
          //重新请求列表数据
            this.reqArr();
        }
      });
    },
    add() {
      reqseckadd(this.user).then(res => {
        if (res.data.code == 200) {
          // 弹成功
          successAlert(res.data.msg);
          //内容清空
          this.empty();
          //弹框消失
          this.info.isshow = false;
          // 重新请求列表
           this.reqArr();
        }
      });
    }
  }
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
</style>